package com.bjpowernode.crm.web.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ResourcesFilter implements Filter {
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) req;
        HttpServletResponse response= (HttpServletResponse) resp;
        //判断发起请求的uri
        String uri=request.getServletPath();
        if ("/login.jsp".equals(uri)||"/settings/user/login.do".equals(uri)){
            filterChain.doFilter(request,response);
            return;
        }
        //判断用户有没有登录，有没有session
        HttpSession session=request.getSession(false);
        if (session!=null){
            filterChain.doFilter(request,response);
            return;
        }
        response.sendRedirect(request.getContextPath()+"/login.jsp");
    }
}
